package main

import (
	"context"
	"fmt"
	"os/exec"
	"runtime"
)

// App struct
type App struct {
	ctx context.Context
}

// NewApp creates a new App application struct
func NewApp() *App {
	return &App{}
}

// startup is called when the app starts. The context is saved
// so we can call the runtime methods
func (a *App) startup(ctx context.Context) {
	a.ctx = ctx
}

// Greet returns a greeting for the given name
func (a *App) Greet(name string) string {
	// result, err := runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
	// 	Type:          runtime.QuestionDialog,
	// 	Title:         "Question",
	// 	Message:       "Do you want to continue?",
	// 	DefaultButton: "No",
	// })
	// fmt.Println(result, err)

	// runtime.N

	// ctx := context.Background()
	// err := runtime.Notify.Show(ctx, runtime.NotifyOptions{
	// 	Title:   "Hello Wails!",
	// 	Message: "This is a desktop notification from Wails.",
	// })
	// if err != nil {
	// 	log.Fatalf("Failed to show notification: %v", err)
	// }

	return fmt.Sprintf("Hello %s, It's show time!", name)
}

// OpenUrl opens the given url in the default browser
func (a *App) OpenUrl(name string) string {
	err := openBrowser(name)
	if err != nil {
		return "Failed to open url"
	}
	return "Url opened successfully"
}

func openBrowser(url string) error {
	var cmd string
	var args []string

	switch runtime.GOOS {
	case "windows":
		cmd = "cmd"
		args = []string{"/c", "start"}
	case "darwin":
		cmd = "open"
	case "linux":
		cmd = "xdg-open"
	default:
		return fmt.Errorf("unsupported platform")
	}

	args = append(args, url)
	return exec.Command(cmd, args...).Start()
}
